package com.example.administrator.runbook.findfragment.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.runbook.R;
import com.example.administrator.runbook.findfragment.model.BookListItem;

import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 俊峰 on 2016/11/9.
 */

public class MyListViewBaseAdapter extends BaseAdapter{

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<BookListItem> mBookListItems;

    public MyListViewBaseAdapter(Context context, List<BookListItem> bookListItems) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(this.mContext);
        mBookListItems = bookListItems;
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (mBookListItems != null) {
            ret = mBookListItems.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        return mBookListItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret;
        if (convertView == null) {
            ret = mLayoutInflater.inflate(R.layout.activity_book_item, parent, false);
        } else {
            ret = convertView;
        }
        ViewHolder holder = (ViewHolder) ret.getTag();
        if (holder == null) {
            holder = new ViewHolder(ret);
        }
        BookListItem item = mBookListItems.get(position);
        onBindView(holder, item);
        return ret;
    }

    private void onBindView(ViewHolder holder, BookListItem item) {
        String format = String.format(Locale.CHINA, "%.2f%%读者留存", item.getRetentionRatio());
        String cover = item.getCover();
        String url = null;
        Pattern compile = Pattern.compile("http:.+");
        Matcher matcher = compile.matcher(cover);
        while (matcher.find()){
            url = matcher.group(0);
            break;
        }
        holder.bookTitle.setText(item.getTitle());
        holder.bookAuthor.setText(item.getAuthor());
        holder.bookType.setText(item.getMajorCate());
        holder.bookContentDes.setText(item.getShortIntro());
        holder.bookFollower.setText(item.getLatelyFollower() + "人在追");
        holder.bookRatio.setText(format);
        Context context = holder.bookIcon.getContext();
        Glide.with(context)
                .load(url)
                .placeholder(R.mipmap.app_icon)
                .into(holder.bookIcon);
    }

    private static class ViewHolder{

        public ImageView bookIcon;
        public TextView bookTitle;
        public TextView bookAuthor;
        public TextView bookType;
        public TextView bookContentDes;
        public TextView bookFollower;
        public TextView bookRatio;

        public ViewHolder(View itemView){
            bookIcon = (ImageView) itemView.findViewById(R.id.activity_book_item_img);
            bookAuthor = (TextView) itemView.findViewById(R.id.activity_book_item_author);
            bookTitle = (TextView) itemView.findViewById(R.id.activity_book_item_title);
            bookType = (TextView) itemView.findViewById(R.id.activity_book_item_type);
            bookContentDes = (TextView) itemView.findViewById(R.id.activity_book_item_textDes);
            bookFollower = (TextView) itemView.findViewById(R.id.activity_book_item_follower);
            bookRatio = (TextView) itemView.findViewById(R.id.activity_book_item_retentionRatio);
        }

    }
}
